import request from 'axios';

import { SWITCH_ACTIVE_FILE, CLOSE_FILE, ADD_FILE, READ_FILE, CREATE_DIR,
    DELETE_PATH, CREATE_FILE, SAVE_FILE, MOVE_PATH, MARK_DIRTY } from '../reducers/files.js';

/**
 * 从路径中获取文件名
 * @param  {string} path
 * @return {string}
 */
function getFileNameFromPath(path) {
    let parts = path.split('/');
    return parts[parts.length - 1];
}

/**
 * 切换当前文件
 * @param  {number} index
 * @return {Object}
 */
export function switchActiveFile(index) {
    return {
        type: SWITCH_ACTIVE_FILE,
        payload: index
    };
}

/**
 * 添加新文件并设为当前
 * @param  {string} path
 * @param  {string} source
 * @return {Function}
 */
export function addAndSetActiveFile(path, source = '') {
    return {
        type: ADD_FILE,
        payload: {
            path,
            name: getFileNameFromPath(path),
            dirty: true
        }
    };
}

/**
 * 读取目录信息
 * @param  {string} path
 * @return {Object}
 */
export function readFile(path) {
    return {
        type: READ_FILE,
        payload: {
            promise: request.get(`/fs/raml${path}`)
                .then(response => response.data)
        },
        meta: {
            path
        }
    };
}

/**
 * 打开文件
 * @param  {string} path
 * @return {Function}
 */
export function openFile(path) {
    return async (dispatch, getState) => {
        const { files } = getState();
        const index  = files.opened.findIndex(
                file => file.path === path);
        let content = '';

        if (index !== -1) {
            // 为已经打开过的文件, 直接切换
            return dispatch(switchActiveFile(index));
        }

        // 打开新文件
        return dispatch(addAndSetActiveFile(path, content));
    };
}

/**
 * 关闭文件
 * @param  {number} index
 * @return {Object}
 */
export function closeFile(index) {
    return {
        type: CLOSE_FILE,
        payload: index
    };
}

/**
 * 创建文件夹
 * @param  {string} path
 * @return {Object}
 */
export function createDir(path) {
    return {
        type: CREATE_DIR,
        payload: {
            promise: request.post(`/fs/raml${path}`,
                {
                    type: 'folder'
                })
                .then(response => response.data)
        }
    };
}

/**
 * 删除文件夹/文件
 * @param  {string} path
 * @return {Object}
 */
export function deletePath(path) {
    return {
        type: DELETE_PATH,
        payload: {
            promise: request.delete(`/fs/raml${path}`)
                .then(response => response.data)
        }
    };
}

/**
 * 重命名文件夹/文件
 * @param  {string} from
 * @param  {string} to
 * @return {Object}
 */
export function movePath(from, to) {
    return {
        type: MOVE_PATH,
        payload: {
            promise: request.put(`/fs/raml${from}`,
                {
                    rename: to
                })
                .then(response => response.data)
        }
    };
}

/**
 * 保存文件
 * @param  {string} path
 * @param  {string} content
 * @return {Object}
 */
export function saveFile(path, content = '') {
    return {
        type: SAVE_FILE,
        payload: {
            promise: request.post(`/fs/raml${path}`,
                {
                    type: 'file',
                    content
                })
                .then(response => response.data)
        }
    };
}

/**
 * 创建文件
 * @param  {string} path
 * @param  {string} content
 * @return {Object}
 */
export function createFile(path) {
    return {
        type: CREATE_FILE,
        payload: {
            promise: request.post(`/fs/raml${path}`,
                {
                    type: 'file',
                    content: ''
                })
                .then(response => response.data)
        }
    };
}

/**
 * 标记文件是否修改过
 * @param  {string} path
 * @param  {bool}   dirty
 * @return {Object}
 */
export function markDirty(path, dirty = false) {
    return {
        type: MARK_DIRTY,
        payload: {
            path,
            dirty
        }
    }
}
